package Gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import DB.UserConnector;

@SuppressWarnings("serial")
public class registerFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_3;

	/**
	 * Create the frame.
	 */
	public registerFrame() {
		setTitle("USER REGISTRATION");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 355, 204);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textField = new JTextField();
		textField.setBounds(181, 11, 138, 20);
		contentPane.add(textField);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setFont(new Font("Symbol", Font.PLAIN, 11));
		textField_1.setColumns(10);
		textField_1.setBounds(181, 73, 138, 20);
		contentPane.add(textField_1);

		textField_3 = new JTextField();
		textField_3.setFont(new Font("Symbol", Font.PLAIN, 11));
		textField_3.setColumns(10);
		textField_3.setBounds(181, 42, 138, 20);
		contentPane.add(textField_3);

		JTextPane txtpnUsername = new JTextPane();
		txtpnUsername.setEditable(false);
		txtpnUsername.setFont(new Font("Tahoma", Font.BOLD, 15));
		txtpnUsername.setBackground(UIManager.getColor("Button.background"));
		txtpnUsername.setText("Name");
		txtpnUsername.setBounds(10, 11, 143, 20);
		contentPane.add(txtpnUsername);

		JTextPane txtpnPassword = new JTextPane();
		txtpnPassword.setEditable(false);
		txtpnPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
		txtpnPassword.setBackground(UIManager.getColor("Button.background"));
		txtpnPassword.setText("Password");
		txtpnPassword.setBounds(10, 42, 143, 20);
		contentPane.add(txtpnPassword);

		JTextPane txtpnConfirmPassword = new JTextPane();
		txtpnConfirmPassword.setEditable(false);
		txtpnConfirmPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
		txtpnConfirmPassword.setBackground(UIManager.getColor("Button.background"));
		txtpnConfirmPassword.setText("Confirm Password");
		txtpnConfirmPassword.setBounds(10, 73, 143, 20);
		contentPane.add(txtpnConfirmPassword);

		JButton btnRegisterAndJoin = new JButton("Register");
		btnRegisterAndJoin.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnRegisterAndJoin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean valid = true;
				String name = textField.getText();
				String password = textField_1.getText();
				String conPass = textField_3.getText();
				if (name.equals("")) {
					JOptionPane.showMessageDialog(null,"Please enter Username");
					valid = false;
				}else
					if (!(password.equals(conPass)) || (password.equals(""))) {
						JOptionPane.showMessageDialog(null,"Password mismatch");
						valid = false;
					}
					else
						if (valid){
							if (App.isPlaying(name)){
								JOptionPane.showMessageDialog(null,name+" is already playing!");
							}
							else{
								int res = UserConnector.getInstance().register(name, password);
								if (res==-1){
									JOptionPane.showMessageDialog(null,"This name is already taken");
								}
								else if(res == -1){
									JOptionPane.showMessageDialog(null,"Error connecting to the database");
								}
								else if (res == 0){
									JOptionPane.showMessageDialog(null,"Registration complete");
									App.webgf.addUserToTable(name);
									App.webgf.setVisible(true);
									App.addPlayer(name);
									setVisible(false);
								}
							}
						}
			}
		}
		);
		btnRegisterAndJoin.setBounds(221, 116, 98, 41);
		contentPane.add(btnRegisterAndJoin);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				if (App.getNumOfPlayers()==0){
					addPlayer addplayer = new addPlayer();
					addplayer.setVisible(true);
				}
				else {
					App.webgf.setVisible(true);
				}
			}
		});
		btnCancel.setBounds(10, 116, 106, 41);
		contentPane.add(btnCancel);
	}
}
